package com.waverider.promotion.biz.dao.ibatis;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.waverider.promotion.biz.dao.OrderDAO;
import com.waverider.promotion.domain.OrderDO;
import com.waverider.promotion.domain.RebateModel;
import com.waverider.promotion.model.OrderQueryModel;

@SuppressWarnings("unchecked")
public class OrderDAOImpl implements OrderDAO {

	private SqlMapClient sqlMapClient;

	@Override
	public void insert(OrderDO order) {
		try {
			sqlMapClient.insert("order.dao.insert", order);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public OrderDO queryByBuyerAndPromotion(Long buyer, Long promotion) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("buyer", buyer);
			map.put("itemId", promotion);
			return (OrderDO) sqlMapClient.queryForObject(
					"order.dao.queryByBuyerAndItemId", map);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public OrderDO queryByOrderId(Long orderId) {
		try {
			return (OrderDO) sqlMapClient.queryForObject(
					"order.dao.queryByOrderId", orderId);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<OrderDO> query(OrderQueryModel queryModel) {
		try {
			return (List<OrderDO>) sqlMapClient.queryForList("order.dao.query",
					queryModel);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public Long count(OrderQueryModel queryModel) {
		try {
			return (Long) sqlMapClient.queryForObject("order.dao.count",
					queryModel);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public boolean update(OrderDO order) {
		try {
			sqlMapClient.startTransaction();
			int rows = sqlMapClient.delete("order.dao.update", order);

			if (rows > 1) {
				throw new RuntimeException("[ERROR] Not possible!");
			}

			sqlMapClient.commitTransaction();

			if (rows == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				sqlMapClient.endTransaction();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	@Override
	public boolean delete(Long id) {
		try {
			sqlMapClient.startTransaction();
			int rows = sqlMapClient.delete("order.dao.delete", id);

			if (rows > 1) {
				throw new RuntimeException("[ERROR] Not possible!");
			}

			sqlMapClient.commitTransaction();

			if (rows == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				sqlMapClient.endTransaction();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	public void setSqlMapClient(SqlMapClient sqlMapClient) {
		this.sqlMapClient = sqlMapClient;
	}

	@Override
	public List<RebateModel> getOrderByShopId(Map<String, Long> params) {
		try {
			return (List<RebateModel>) sqlMapClient.queryForList("order.dao.queryByShopId", params);
		} catch (SQLException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}
}
